//
//  BSActivityViewController.m
//  BS
//
//  Created by 王浩宇 on 15/8/21.
//  Copyright (c) 2015年 lanou. All rights reserved.
//

#import "BSActivityViewController.h"
#import "BSActivityCollectionViewCell.h"
#import "Header.h"
#import "BSActivityModel.h"
#import "LJRequestManagerBlock.h"
#import "BSActivityDatailsViewController.h"

@interface BSActivityViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property (nonatomic, strong)UICollectionView *collectionView;

@property (nonatomic, strong)NSMutableArray *activityArray;


@end

@implementation BSActivityViewController
- (NSMutableArray *)activityArray
{
    if (_activityArray == nil) {
        _activityArray = [NSMutableArray arrayWithCapacity:0];
    }
    return _activityArray;
}
- (void)finaData
{
    [LJRequestManagerBlock manager:GET requestWith:@"http://api.budejie.com/api/api_open.php?a=themes&appname=baisishequ&asid=9A2EA4AE-2DEB-4479-A977-8CB5B3EFF60F&c=topic&client=iphone&device=ios%20%E8%AE%BE%E5%A4%87&jbk=0&mac=&market=&openudid=f60e4fa5905641dfd3bec86efbf33da57105acc6&page=1&pagesize=20&sex=m&udid=&ver=3.6.1" dic:nil finish:^(NSData *data) {
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSArray *listArray = dict[@"list"];
        for (NSDictionary *dic in listArray) {
            BSActivityModel *model = [[BSActivityModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.activityArray addObject:model];
        }
        [self.collectionView reloadData];
    } error:^(NSError *error) {
        
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self finaData];
    [self creationCollectionView];
    // Do any additional setup after loading the view.
}

- (void)creationCollectionView
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(SCREEN_WIDTH/ 2 - 15, 185);
    layout.minimumInteritemSpacing = 5;
    layout.minimumLineSpacing = 5;
    _collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
    _collectionView.backgroundColor = [UIColor whiteColor];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    
    [_collectionView registerNib:[UINib nibWithNibName:@"BSActivityCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"cell"];
    
    [self.view addSubview:_collectionView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _activityArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    BSActivityCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    BSActivityModel *model = _activityArray[indexPath.row];
    cell.model = model;
    return cell;
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(0, 10, 0, 10);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    BSActivityDatailsViewController *datalisVC = [[BSActivityDatailsViewController alloc] init];
    BSActivityModel *model = _activityArray[indexPath.row];
    datalisVC.themeID = model.theme_id;
    datalisVC.themeName = model.theme_name;
    [self.navigationController pushViewController:datalisVC animated:YES];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
